var left_margin_step = 15;
var pickCallBackFunction = null;
    // 点击obj，弹出参照，并加载采购组织
    // 入参：obj表示点击它弹出参照; callbackfunction表示选确定后调用的函数
	function showRefPanel(obj, callbackfuction) {
		pickCallBackFunction = callbackfuction;
		
		var myts = new Date();
		var formStr = $("#groupUnitPicker").find("#org_filter").serialize();
		jQuery.ajax( {
			type : "POST",
			async : false,
			url : ebvp_context + "/register/qrySupGroup?myts=" + myts,
			dataType : "text",
			data : "&" + formStr,
			success : function(result) {
				if (result != "[]") {
					var orgArr = eval(result);
					$("#groupUnitPicker").find(".tree_list").empty();
					for ( var loop = 0; loop < orgArr.length; loop++) {
						var obj = orgArr[loop];
						var a = $("<a></a>").addClass("category_trigger").attr(
								"margin", 0);
						var inputObj = $("<input/>").val(obj.pk_org).attr(
								"type", "radio").attr("name", "pk_org_intree").attr("disabled","disabled");
						var label = $("<label></label>").text(obj.name);
						var inputObj1 = $("<input/>").val(obj.pk_group).attr(
								"type", "hidden").attr("name",
								"pk_group_intree");
						var div = $("<div></div>").addClass("tree_category")
								.append(a).append(inputObj).append(label)
								.append(inputObj1);
						$("#groupUnitPicker").find(".tree_list").append(div);
					}
				}
			}
		});
		
		var position = $(obj).offset();
		position.top += 22;
		$("#groupUnitPicker").slideDown("normal");
		$("#groupUnitPicker").offset(position);
	}
	
var OrgList = function() {
	
	// 过滤采购组织
	$("#filterPurOrg")
			.bind(
					'click',
					function() {
						var myts = new Date();
						var formStr = $(this).parents("div")
								.find("#org_filter").serialize();
						jQuery
								.ajax( {
									type : "POST",
									async : false,
									url : ebvp_context
											+ "/register/qryAllGroup?myts="
											+ myts,
									dataType : "text",
									data : "&" + formStr,
									success : function(result) {
										if (result != "[]") {
											var orgArr = eval(result);
											$("#groupUnitPicker").find(
													".tree_list").empty();
											for ( var loop = 0; loop < orgArr.length; loop++) {
												var obj = orgArr[loop];
												var a = $("<a></a>").addClass(
														"category_trigger")
														.attr("margin", 0);
												var inputObj = $("<input/>")
														.val(obj.pk_org)
														.attr("type", "radio")
														.attr("name",
																"pk_org_intree");
												var label = $("<label></label>")
														.text(obj.name);
												var inputObj1 = $("<input/>")
														.val(obj.pk_group)
														.attr("type", "hidden")
														.attr("name",
																"pk_group_intree");
												var div = $("<div></div>")
														.addClass(
																"tree_category")
														.append(a).append(
																inputObj)
														.append(label).append(
																inputObj1);
												$("#groupUnitPicker").find(
														".tree_list").append(
														div);
											}
										}
									}
								});
					});

	$(".category_trigger")
			.live(
					'click',
					function() {
						var category_triggerObj = $(this);
						if (category_triggerObj.hasClass("close")) {
							category_triggerObj.parent(".tree_category").next(
									".children").slideUp(function() {
								category_triggerObj.removeClass("close");
							});
							return false;
						}

						$(this).addClass("close");
						var pk_org_val = $(this).next("input").val();
						var pk_group_val = $(this).next("input").next("label")
								.next("input").val();
						var myts = new Date();
						var formStr = $(this).parents("div")
								.find("#org_filter").serialize();
						jQuery
								.ajax( {
									type : "POST",
									async : false,
									url : ebvp_context
											+ "/register/qrySubPurchaseOrgByParentPk?pk_org="
											+ pk_org_val + "&pk_group="
											+ pk_group_val + "&myts=" + myts,
									dataType : "text",
									data : "&" + formStr,
									success : function(result) {
										var orgArr = eval(result);

										var current_margin_left_size = parseInt(category_triggerObj
												.attr("margin"))
												+ left_margin_step;

										var childrenDiv = $("<div></div>")
												.addClass("children");
										for ( var loop = 0; loop < orgArr.length; loop++) {
											var obj = orgArr[loop];

											var a = $("<a></a>")
													.addClass(
															"category_trigger")
													.attr(
															"style",
															"margin-left:"
																	+ current_margin_left_size
																	+ "px")
													.attr("margin",
															current_margin_left_size);
											var inputObj = $("<input/>").val(
													obj.pk_org).attr("type",
													"radio").attr("name",
													"pk_org_intree");
											if (obj.isPurchaseOrg == "N") {
												inputObj.attr("disabled",
														"disabled");
											}
											var label = $("<label></label>")
													.text(obj.name);
											var inputObj1 = $("<input/>").val(
													obj.pk_group).attr("type",
													"hidden").attr("name",
													"pk_group_intree");
											var div = $("<div></div>")
													.addClass("tree_category")
													.append(a).append(inputObj)
													.append(label).append(
															inputObj1);
											childrenDiv.append(div);
										}
										childrenDiv
												.insertAfter(category_triggerObj
														.parent());
									}
								});

						category_triggerObj.parent(".tree_category").siblings(
								".tree_category").next(".children").slideUp(
								function() {
									$(this).prev(".tree_category").find(
											".category_trigger").removeClass(
											"close");
								});

						return false;

					});

	$("#groupPicked").live('click', function() {
		var items = $("#groupUnitPicker").find("[name='pk_org_intree']");
		if(items != null){
			for(var loop=0; loop<items.length; loop++){
				if(items[loop].checked == true){
					var pk = $(items[loop]).val();
					var name = $(items[loop]).next("label").html();
					pickCallBackFunction(pk, name);
					break;
				}
			}
		}
		$("#groupUnitPicker").slideUp();
	});

	$("#groupCancel").live('click', function() {
		$("#groupUnitPicker").slideUp();
	});

	this.init = function() {};
}

$(function() {
	var orgListInstance = new OrgList();
	orgListInstance.init();
});
